﻿using App_Clinique.ClassLibrary.Classes;
using App_Clinique.ClassLibrary.DbContext;
using App_Clinique.ClassLibrary.Entities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace App_Clinique
{
    public partial class FormStart : Form
    {
        public static MainForm EcranPrincipal { get; set; }

        public static String Licence = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Ressources", "login.lic");
        public static String LicencePreCryptage = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Ressources", "loginPreCryptage.lic");

        public static String OrdonnacesBinairePreCryptage = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Ressources", "OrdonnacesBinairePreCryptage");
        public static String OrdonnacesBinaire = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Ressources", "Ordonnances");

        public static String SauvegardeZIP = "C:\\Medocs\\Backup\\sauvegarde.zip";
        public static String SauvegardeTemp = "C:\\Medocs\\Backup\\Sauvegarde\\";
        public static String SauvegardeBDD = "C:\\Medocs\\Backup\\Sauvegarde\\App_cliniqueContext.bak";
        public static String SauvegardeDocumentsPatients = "C:\\Medocs\\Backup\\Sauvegarde\\App_cliniqueContext.bak";

        public FormStart()
        {
            InitializeComponent();
        }

        /****************************************************************************************************
         * Methods
         ***************************************************************************************************/
        public void ActiveApp()
        {
            EcranPrincipal.Show();
            EcranPrincipal.WindowState = FormWindowState.Maximized;
        }

        /****************************************************************************************************
         * Events
         ***************************************************************************************************/
        private void FormStart_Load(object sender, EventArgs e)
        {
            AuthentificationForm EcranAuthentification = new AuthentificationForm();
            EcranAuthentification.MdiParent = this;
            EcranAuthentification.Show();

            EcranPrincipal = new MainForm();
            EcranPrincipal.MdiParent = this;
        }

        private void référentielDesMédicamentsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MedicamentForm EcranReferentielMedicaments = new MedicamentForm();
            EcranReferentielMedicaments.ShowDialog();
        }

        private void lesOrdonnancesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Singleton.Current.Ordonnances = Ordonnance.GetList();
            Singleton.Current.Ordonnances.ForEach(o => {
                o.Patient = Patient.Get(o.PatientID);
                o.OrdonnancesMedicaments = OrdonnancesMedicaments.GetList(o.DocumentID);
                o.OrdonnancesMedicaments.ToList().ForEach(om => {
                    om.Medicament = Medicament.Get(om.MedicamentID);
                });
            });

            String strKey = "abc123deaoezdf77";
            String strIv = "abc123deaoezdf77";

            BinaryFormatter bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            bf.Serialize(ms, Singleton.Current.Ordonnances);
            Byte[] octets = ms.ToArray();
            Directory.CreateDirectory(Path.GetDirectoryName(OrdonnacesBinaire));
            File.WriteAllBytes(OrdonnacesBinairePreCryptage, octets);

            // cryptage du fichier
            Tools.Libraries.AesRjindael.EncryptFile(strKey, strIv, OrdonnacesBinairePreCryptage, OrdonnacesBinaire);
            File.Delete(OrdonnacesBinairePreCryptage);

            MessageBox.Show("Sauvegarde effectuée !");
        }

        private void laBaseDeDonnéesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // on cré le dossier qui devra être compressé
            Directory.CreateDirectory(Path.GetDirectoryName(SauvegardeTemp));

            // on cré le backup de la BDD
            Directory.CreateDirectory(Path.GetDirectoryName(SauvegardeBDD));
            App_cliniqueContext db = new App_cliniqueContext();
            db.BackUp(SauvegardeBDD);

            // on efface le fichier ZIP s'il existe
            if (File.Exists(SauvegardeZIP))
            {
                File.Delete(SauvegardeZIP);
            }

            // on cré le fichier ZIP
            ZipFile.CreateFromDirectory(SauvegardeTemp, SauvegardeZIP, CompressionLevel.Fastest, false);

            // on détruit les fichiers temporaires
            Directory.Delete(Path.GetDirectoryName(SauvegardeTemp), true);

            MessageBox.Show("Sauvegarde effectuée !");
        }
    }
}
